<?php

/**
 * Description of Technologie
 *
 * @author jey
 * @version 1.0
 */
class Technologie
{

    /**
     * @var int
     */
    protected $IdTechnologie;
    /**
     * @var string
     */
    protected $NomTechnologie;
    /**
     * @var bool
     */
    public $requeteOk;
    /**
     * @var string
     */
    public $errormsg;

    /**
     * @param int $idT
     */
    function __construct($idT = 0)
    {
        if (isset($idT) && !empty($idT))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "SELECT Technologie.* FROM Technologie WHERE idTechnologie = :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $idT, PDO::PARAM_INT);

            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);

                $this->IdTechnologie = $result->idTechnologie;
                $this->NomTechnologie = $result->NomTechnologie;

                $this->errormsg = "Technologie recuperee";
                $this->requeteOk = true;
            }
            else
            {
                $this->IdTechnologie = 0;
                $this->NomTechnologie = "";

                $this->errormsg = "SQL Failed";
                $this->requeteOk = false;
            }
            $c->closeCursor();
        }
        else
        {
            $this->IdTechnologie = 0;
            $this->NomTechnologie = "";

            $this->errormsg = "Technologie vierge";
        }
    }

    /**
     * @return bool
     */
    function delete()
    {
        if (!empty($this->IdTechnologie))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "DELETE FROM Technologie WHERE idTechnologie :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $this->IdTechnologie, PDO::PARAM_INT);

            if ($c->execute())
            {
                return true;
            }
            else
            {
                $this->errormsg = $c->errorInfo();
                $this->requeteOk = false;
            }

            $c->closeCursor();
        }

        return false;
    }

    /**
     * @return int Identifiant de la technologie
     */
    public function save()
    {
        if (!empty($this->NomTechnologie))
        {
            $sql = SQL::getInstance()->getConnection();

            if (!empty($this->IdTechnologie))
            {
                // Technologie définit on "update"
                $query = "UPDATE Technologie
                         SET
                            NomTechnologie = :nom
                         WHERE idTechnologie = :id";
                $sql->beginTransaction();
                try
                {
                    $c = $sql->prepare($query);
                    $c->bindParam(":id", $this->IdTechnologie, PDO::PARAM_INT);

                    $c->bindParam(":nom", $this->NomTechnologie, PDO::PARAM_STR, 255);

                    if ($c->execute())
                    {
                        $sql->commit();
                        $this->requeteOk = true;
                    }
                    else
                    {
                        $sql->rollBack();
                        $this->requeteOk = false;
                        $this->errormsg = $c->errorInfo();
                    }

                    $c->closeCursor();
                }
                catch (PDOException $ex)
                {
                    $this->errormsg = $ex->getMessage();
                    $this->requeteOk = false;
                }
            }
            else
            {
                // Nouveau Technologie on peut "insert"
                $query = "INSERT INTO Technologie
                            (NomTechnologie)
                          VALUES(:nom)";

                $sql->beginTransaction();
                try
                {
                    $c = $sql->prepare($query);

                    $c->bindParam(":nom", $this->NomTechnologie, PDO::PARAM_STR, 255);

                    if ($c->execute())
                    {
                        $sql->commit();
                        $this->IdTechnologie = $sql->lastInsertId();
                        $this->requeteOk = true;
                    }
                    else
                    {
                        $sql->rollBack();
                        $this->requeteOk = false;
                        $this->errormsg = $c->errorInfo();
                    }

                    $c->closeCursor();
                }
                catch (PDOException $ex)
                {
                    $this->errormsg = $ex->getMessage();
                    $this->requeteOk = false;
                }
            }
            return $this->IdTechnologie;
        }
        else
            return 0;
    }

    /**
     *
     * @return int
     */
    public function getIdTechnologie()
    {
        return $this->IdTechnologie;
    }

    /**
     *
     * @return string
     */
    public function getNomTechnologie()
    {
        return $this->NomTechnologie;
    }

    /**
     *
     * @param string $NomTechnologie
     * @return array
     */
    public function setNomTechnologie($NomTechnologie)
    {
        $err = array("bool" => true, "msg" => "");
        if (isset($NomTechnologie) && !empty($NomTechnologie))
        {
            if (strlen($NomTechnologie) < 255)
            {
                $this->NomTechnologie = htmlentities($NomTechnologie);
            }
            else
            {
                $err["bool"] = false;
                $err["msg"] = "Nom trop long";
            }
        }
        else
        {
            $err["bool"] = false;
            $err["msg"] = "Nom vide";
        }
        return $err;
    }

    /**
     * @param int $limit
     * @param int $begin
     * @return array Array de Technologie
     */
    public function getList($limit=0, $begin=0)
    {
        if (empty($limit))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT * FROM Technologie';
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT *
                              FROM Technologie';

            $query .= ' LIMIT :begin,:limit';
            $c = $sql->prepare($query);
            $c->bindParam(':begin', $begin, PDO::PARAM_INT);
            $c->bindParam(':limit', $limit, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
    }

    /**
     * @static
     * @return int Nombre de Technologie
     */
    static public function globalCount()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = 'SELECT COUNT(idTechnologie) as count
				  FROM Technologie';

        $c = $sql->prepare($query);
        if ($c->execute())
        {
            $result = $c->fetch(PDO::FETCH_OBJ);
            return $result->count;
        }
        else
        {
            return 0;
        }
    }

    /**
     * @static
     * @param int $id
     * @return string Nom de la technologie
     */
    static public function getNomById($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "SELECT NomTechnologie FROM Technologie WHERE IdTechnologie = :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetch(PDO::FETCH_OBJ);
            return $result->NomTechnologie;
        }
    }

}

?>
